﻿#include "Rod.h"
#include "Resource.h"

Rod::Rod(LPDIRECT3DDEVICE9 d3ddv, LPDIRECT3DSURFACE9 BackBuffer, LPD3DXSPRITE SpriteHandler, float x, float y, bool isLeft, int rod_version) : Object(x, y)
{	 

	p_sprite_rod_v1 = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[3]->getFileName()), 125, 0);
	p_sprite_rod_v2 = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[32]->getFileName()), 125, 0);
	p_sprite_rod_v3 = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[33]->getFileName()), 125, 0);

	if (rod_version == 1)
	{
		p_sprite = p_sprite_rod_v1;
	}
	else if (rod_version == 2)
	{
		p_sprite = p_sprite_rod_v2;
	}
	else if (rod_version == 3)
	{
		p_sprite = p_sprite_rod_v3;
	}
	width = p_sprite->getTexture()->getWidth();
	height = p_sprite->getTexture()->getHeight();

	this->pos_x_old = x;
	this->pos_y_old = y;

	if (isLeft == true)
	{
		p_sprite->setFrame(0,3);
		p_sprite->setIndex(0);
		this->is_left = true;
	} 
	else
	{
		p_sprite->setFrame(3,6);
		p_sprite->setIndex(3);
		this->is_left = false;
	}
	p_sprite->setIndexFrame(0);
	 
	obj_state = IS_ACTIVING;
	num_score = 0;
}

Rod::~Rod()
{
	if (p_sprite != NULL)
	{
		delete p_sprite;
	}
}

void Rod::render(int cameraX, int cameraY)
{
	if (obj_state != IS_REMOVE)
	{
		if (is_left == true)
		{
			int _countFrameRod = p_sprite->getIndexFrame();
			if (_countFrameRod <= 1)
			{
				p_sprite->render(pos_x + 32, pos_y, cameraX, cameraY); //32:width of simon
			}
			else if (_countFrameRod > 1)
			{
				p_sprite->render(pos_x - 12 - width, pos_y, cameraX, cameraY); //width: rod
			}

		}
		else
		{
			int _countFrameRod = p_sprite->getIndexFrame();
			if (_countFrameRod <= 1)
			{
				p_sprite->render(pos_x - 12 - width, pos_y, cameraX, cameraY); 
			}
			else if (_countFrameRod > 1)
			{
				p_sprite->render(pos_x + 32, pos_y, cameraX, cameraY); //32
			}

		}
		//p_sprite->render(pos_x, pos_y, cameraX, cameraY);
	}

}

OBJECT_TYPE Rod::getObjectType()
{
	return OBJECT_ITEM;
}


void Rod::update(list<Object*>* obj_list, DxInput* input, float gameTime)
{
	switch(obj_state)
	{
	case IS_IDLE:
		
		break;

	case IS_ACTIVING:

		p_sprite->update(gameTime);
		count_frame_rod = p_sprite->getIndexFrame();
		if (count_frame_rod >= 3)
		{
			p_sprite->setIndexFrame(0);

			count_frame_rod = 0;

			changeState(IS_REMOVE);
		}

		break;


	}

	//p_sprite->update(gameTime);

}

void Rod::updatePosition(list<Object*>* obj_list, DxInput* input, float gameTime, int pos_x, int pos_y)
{
	switch(obj_state)
	{
	case IS_IDLE:
		
		break;

	case IS_ACTIVING:
		this->pos_x_old = this->pos_x;
		this->pos_y_old = this->pos_y;

		this->pos_x = pos_x;
		this->pos_y = pos_y;

		p_sprite->update(gameTime);
		count_frame_rod = p_sprite->getIndexFrame();
		if (count_frame_rod == 2)
		{
			attack(obj_list, gameTime);
		}
		else if (count_frame_rod >= 3)
		{
			p_sprite->setIndexFrame(0);

			count_frame_rod = 0;

			changeState(IS_REMOVE);
		}
		this->pos_x_old = this->pos_x;
		this->pos_y_old = this->pos_y;
		break;


	}

	//p_sprite->update(gameTime);

}


 

void Rod::changeState(int state)
{
	obj_state = state;
	switch(state)
	{
	case IS_ACTIVING:		
		break;	 

	case IS_REMOVE:
		break;
	}
}

void Rod::attack(list<Object*>* obj_list, float time)
{
	Object *obj;
	list<Object*>::iterator i_obj;

	if (is_left == true)
	{
		pos_x_old = pos_x_old - width;
	}
	else
	{
		pos_x_old = pos_x_old + width; //32;
	}

	//Va cham Thong Thuong
	for (i_obj = obj_list->begin(); i_obj != obj_list->end(); i_obj++)
	{
		obj = *i_obj;

		if (obj->getObjectType() == OBJECT_ENEMY || obj->getObjectType() == OBJECT_ITEM || obj->getObjectType() == OBJECT_LAND)		
		{ 
			Box block(this->pos_x_old,
				this->pos_y_old - this->height/2, 
				this->width, 
				this->height/4,
				this->pos_x - this->pos_x_old,
				this->pos_y - this->pos_y_old);

			Box box(obj->getPosX(),
				obj->getPosY() - obj->getHeight(),
				obj->getWidth(),
				obj->getHeight()
				//obj->getVelocX() * time,
				//obj->getVelocY() * time + 1/2.0 * obj->getAccelY() * time * time
				);

			

			Box boxphase = Collision::GetSweptBroadphaseBox(block);			 
			//if (Collision::AABBCheck(boxphase, block)){}

			if (Collision::AABBCheck(boxphase, box))//if (Collision::RectVsRect(box, block))
			{
				switch (obj->getIDObjType())
				{
				case 11:	//ghost
					{
						if (obj->getObjState() == ES_ACTIVING)
						{
							obj->changeState(ES_DIED);
							num_score = 100;
						}
					}
					break;

				case 121:	//dog
				case 122:
					{
						if (obj->getObjState() == ES_ACTIVING)
						{
							obj->changeState(ES_DIED);
							num_score = 100;
						}
					}
					break;

				case 13:	//bat boss
				case 18:	//head boss
					{
						if (obj->getObjState() == ES_ACTIVING)
						{
							//obj->changeState(ES_DIED);
							int blood = obj->getBlood();
							blood--;
							obj->setBlood(blood);
							if (obj->getBlood() == 0)
							{
								obj->changeState(ES_DIED);
							}
							else
							{
								obj->changeState(ES_PAUSE);
							}
						}
					}
					break;

				case 14:	//red bat enemy
					{
						if (obj->getObjState() == ES_ACTIVING)
						{
							obj->changeState(ES_DIED);
							num_score = 100;
						}
					}
					break;

				case 15:	//red dragon enemy
					{
						if (obj->getObjState() == ES_ACTIVING)
						{
							obj->changeState(ES_DIED);
							num_score = 100;
						}
					}
					break;

				case 16:	//Stone knight
					{
						if (obj->getObjState() == ES_ACTIVING)
						{
							obj->changeState(ES_DIED);
							num_score = 100;
						}
					}
					break;

				case 17:	//head dragon
					{
						if (obj->getObjState() == ES_ACTIVING)
						{
							obj->changeState(ES_DIED);
							//num_score = 100;
						}
					}
					break;

				//case 21: //Heart
				//	{
				//		if (obj->getObjState() == IS_IDLE)
				//		{
				//			obj->changeState(IS_ACTIVING); 
				//		}

				//	}
				//	break;

				case 31: //1 Heart - candle
					{
						if (obj->getObjState() == IS_IDLE)
						{
							obj->changeState(IS_EXPLOSION); 
						}

					}
					break;

				case 32: //5 Heart - candle
					{
						if (obj->getObjState() == IS_IDLE)
						{
							obj->changeState(IS_EXPLOSION); 
						}

					}
					break;
				case 321: //5 Heart - pot
					{
						if (obj->getObjState() == IS_IDLE)
						{
							obj->changeState(IS_EXPLOSION); 
						}

					}
					break;

				case 33: //Whip - pot
					{
						if (obj->getObjState() == IS_IDLE)
						{
							obj->changeState(IS_EXPLOSION); 
						}

					}
					break;

				case 34:	//Dagger - pot
				case 341:	//Dagger - candle
					{
						if (obj->getObjState() == IS_IDLE)
						{
							obj->changeState(IS_EXPLOSION); 
						}

					}
					break;

				case 35: //400 pts - candle
					{
						if (obj->getObjState() == IS_IDLE)
						{
							obj->changeState(IS_EXPLOSION); 
						}

					}
					break;

				case 36: //700 pts - candle
					{
						if (obj->getObjState() == IS_IDLE)
						{
							obj->changeState(IS_EXPLOSION); 
						}

					}
					break;

				case 37: //1000 pts - candle
					{
						if (obj->getObjState() == IS_IDLE)
						{
							obj->changeState(IS_EXPLOSION); 
						}

					}
					break;

				case 38: //Holy water - candle
					{
						if (obj->getObjState() == IS_IDLE)
						{
							obj->changeState(IS_EXPLOSION); 
						}

					}
					break;

				case 39: //Holy cross - candle
					{
						if (obj->getObjState() == IS_IDLE)
						{
							obj->changeState(IS_EXPLOSION); 
						}

					}
					break;

				case 40: //Stop watch - candle
					{
						if (obj->getObjState() == IS_IDLE)
						{
							obj->changeState(IS_EXPLOSION); 
						}

					}
					break;

				case 41: //food
					{
						if (obj->getObjState() == IS_IDLE)
						{
							obj->changeState(IS_ACTIVING);
						}
					}
					break;

				case 42: //Axe - candle
					{
						if (obj->getObjState() == IS_IDLE)
						{
							obj->changeState(IS_EXPLOSION); 
						}

					}
					break;

				case 43: //Brick wall
				case 431:
					{
						if (obj->getObjState() == BS_NORMAL)
						{
							obj->changeState(BS_BREAKING); 
						}
					}
					break;

				case 45: //Boomerang - candle
					{
						if (obj->getObjState() == IS_IDLE)
						{
							obj->changeState(IS_EXPLOSION); 
						}
					}
					break;


				}

			}//end if

		}//end if

	}//end for
}